# -*- coding: utf8 -*-

import unittest

from google.appengine.api import memcache
from google.appengine.ext import ndb
from google.appengine.ext import testbed

from game_template import *

JSON_EXAMPLE = u"""{
    "hat": [
            {
                    "owner": null,
                    "text": "text"
            },
            {
                    "owner": null,
                    "text": "hat"
            }
    ],
    "players": [
            "Andrey",
            "Charlie"
    ],
    "remote_id": 7,
    "settings": {
        "skip_words_per_round": 0,
        "time_before_out_sec": 3,
        "time_per_round_sec": 20,
        "word_count": null
    }
}"""


class GameTemplateTest(unittest.TestCase):
    def setUp(self):
        self.players = [u'Andrey', u'Charlie']
        self.words = [u'text', u'hat']
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.maxDiff = 1000
        self.settings = Settings()
        self.settings.time_per_round_sec = 20
        self.settings.skip_words_per_round = 0
        self.settings.time_before_out_sec = 3

    def test_construct(self):
        game_template = GameTemplate(players=self.players,
                                     words=self.words, id_=7,
                                     settings=self.settings,
                                     pin_code=3057)
        self.assertEqual(game_template.players, self.players)
        self.assertEqual(game_template.words, self.words)

    def test_json(self):
        game_template = GameTemplate(players=self.players,
                                     words=self.words, id_=7,
                                     settings=self.settings,
                                     pin_code=3057)
        json_got = game_template.make_json()
        self.assertEqual(type(json_got), unicode)
        json_got = self.remove_spaces(json_got)
        json_needed = self.remove_spaces(JSON_EXAMPLE)
        self.assertEqual(json_got, json_needed)

    def remove_spaces(self, text):
        text = text.replace(' ', '')
        text = text.replace('\n', '')
        text = text.replace('\r', '')
        text = text.replace('\t', '')
        return text

    def test_adding(self):
        id_1 = GameTemplate.get_new_id()
        self.assertEqual(id_1, 0)
        game_template_1 = GameTemplate(players=self.players,
                                       words=self.words, id_=id_1,
                                       settings=self.settings,
                                       pin_code=3057)
        game_template_1.put()
        id_2 = GameTemplate.get_new_id()
        self.assertEqual(id_2, 1)
        game_template_2 = GameTemplate(players=self.players,
                                       words=self.words, id_=id_2,
                                       settings=self.settings,
                                       pin_code=3057)
        game_template_2.put()
        memcache.delete('game_id_count')
        id_3 = GameTemplate.get_new_id()
        self.assertEqual(id_3, 2)
        game_template_3 = GameTemplate(players=self.players,
                                       words=self.words, id_=id_3,
                                       settings=self.settings,
                                       pin_code=3057)
        game_template_3.put()
        self.assertEqual(3, len(list(GameTemplate.query())))
        self.assertEqual(self.players, GameTemplate.query().get().players)
        self.assertEqual(memcache.get('game_by_id_2'), game_template_3.key)

    def tearDown(self):
        self.testbed.deactivate()


if __name__ == "__main__":
    unittest.main()
